package flextv.core.logic {

import com.adobe.air.logging.FileTarget;

import flash.filesystem.File;

import mx.logging.ILogger;
import mx.logging.Log;
import mx.logging.LogEventLevel;
import mx.logging.targets.LineFormattedTarget;
import mx.logging.targets.TraceTarget;
	

public class Logger {
	
	private static var INSTANCE: Logger;
	
	 
	public function Logger(singletonEnforcer: SingletonEnforcer) {
		prepareTarget(new TraceTarget());
		
		const logFile: File = File.applicationStorageDirectory.resolvePath("FlexTV-" + (new Date().toLocaleDateString()) + ".log");
		trace("Path of LOG file is [" + logFile.nativePath + "].");
		prepareTarget(new FileTarget(logFile)); // FileTarget from as3corelib
	}
	
	private static function prepareTarget(target: LineFormattedTarget): void {
		target.level = LogEventLevel.ALL;
		target.filters = [ "flextv.*" ];
		target.includeDate = true;
		target.includeTime = true;
		target.includeLevel = true;
		target.includeCategory = true;
		
		Log.addTarget(target);
	} 
	
	public static function getLog(clazz: Class): ILogger {
		return Logger.instance.newILogger(clazz);
	}
	
	private function newILogger(clazz: Class): ILogger {
		if(clazz == null) throw new ArgumentError("clazz == null");
		return Log.getLogger(Util.getFullQualifiedClassName(clazz));
	}
	
	
	private static function get instance(): Logger {
		if(INSTANCE == null) { INSTANCE = new Logger(new SingletonEnforcer()); }
		return INSTANCE;
	}

}
}

class SingletonEnforcer { }
